﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HYSoft.BLL;

namespace HYSoft.Web
{
    public partial class equipment_add99 : System.Web.UI.Page
    {
        string adminnoss = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            adminnoss = Request.QueryString["adminnos"];
            if (Context.Session["uid"] == null || Context.Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            string units1 = "";
            if (!IsPostBack)
            {
                DataTable dt = new usr2monitor().GetList("[user_index]=" + Session["uid"]).Tables[0];
                DropDownList2.DataSource = dt.DefaultView;
                DropDownList2.DataTextField = "addr";
                DropDownList2.DataValueField = "index";
                DropDownList2.DataBind();
                dt.Clear();
                dt.Dispose();
                DataTable dt3 = new user().GetList("[index]=" + Session["uid"]).Tables[0];
                units1 = dt3.Rows[0]["unit"].ToString();
                dt3.Clear();
                dt3.Dispose();
                //传感器的绑定
                string sql = " [index]!=0";
                DataTable dt1 = new BLL.sensor_node().GetList(sql).Tables[0];
                if (dt1.Rows.Count > 0)
                {
                    DropDownList3.DataSource = dt1.DefaultView;
                    DropDownList3.DataTextField = "name";
                    DropDownList3.DataValueField = "adminno";
                    DropDownList3.DataBind();

                    DropDownList3.Items.FindByValue(adminnoss).Selected = true;
                    dt1.Clear();
                    dt1.Dispose();
                }
                DataTable dt2 = new user().GetList("[unit]='" + units1 + "' and [type]=0 and [intvalue2]=1 ").Tables[0];
                if (dt2.Rows.Count > 0)
                {
                    DropDownList12.DataSource = dt2.DefaultView;
                    DropDownList12.DataTextField = "name";
                    DropDownList12.DataValueField = "index";
                    DropDownList12.DataBind();
                    dt2.Clear();
                    dt2.Dispose();
                }
                mac_list();
            }
        }

        private void mac_list()
        {
            DataTable dt2 = new mac_value().GetList("[unit_index]=" + Session["uid"] + " and [level2]=0 and [adminno]= " + DropDownList3.SelectedValue).Tables[0];
            if (dt2.Rows.Count > 0)
            {
                DropDownList13.DataSource = dt2.DefaultView;
                DropDownList13.DataTextField = "mac";
                DropDownList13.DataValueField = "mac";
                DropDownList13.DataBind();
                dt2.Clear();
                dt2.Dispose();
            }
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string nodetype = DropDownList3.SelectedValue.ToString();
            //转换到相应的页面
            Response.Write("<script>window.location = 'equipment_add" + nodetype + ".aspx?adminnos=" + nodetype + "';</script>");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int text14 = int.Parse(this.TextBox14.Value);
            string fileNamee1 = "";
            string fileNamee = FileUpload1.FileName.ToString();
            int imgflag = 1;
            if (fileNamee == "")
            {
                fileNamee = "Displacementmeter.jpg";
                fileNamee1 = "defaultimg/" + fileNamee;
                imgflag = 0;
            }
            else
            {
                fileNamee1 = "files/" + fileNamee;
            }
            string text16 = this.TextBox16.Value;
            string text17 = this.TextBox17.Value;
            string list2text = this.DropDownList2.SelectedValue;
            string list12text = this.DropDownList12.SelectedValue;
            string text4 = this.TextBox2.Value;
            string otherdefault = "'正常','正常','正常','正常','正常'";
            string typ3 = fileNamee.Substring(fileNamee.LastIndexOf(".") + 1);//上传文件的后缀名
            typ3 = typ3.ToLower();
            if (typ3 == "gif" || typ3 == "jpg" || typ3 == "png")
            {
                if (imgflag == 1)
                    FileUpload1.SaveAs(Server.MapPath("files/") + fileNamee);
                Model.gprsNode gprsNodemodel = new Model.gprsNode();
                gprsNodemodel.adminno = 99;
                gprsNodemodel.tux = fileNamee1;
                gprsNodemodel.areano = Convert.ToInt32(list2text);
                gprsNodemodel.intvalue3 = 0;
                gprsNodemodel.mac = Convert.ToInt32(DropDownList13.SelectedValue);
                gprsNodemodel.simno = text16;
                gprsNodemodel.addr = text17;
                gprsNodemodel.gongdian = text4;
                gprsNodemodel.rate0 = text14;
                gprsNodemodel.wendu = otherdefault;
                new gprsNode().Add(gprsNodemodel);

                Model.node_to_user node_to_usermodel = new Model.node_to_user();
                node_to_usermodel.user_index = Convert.ToInt32(list12text);
                node_to_usermodel.mac = Convert.ToInt32(DropDownList13.SelectedValue);
                new node_to_user().Add(node_to_usermodel);

                Model.mac_value mac_valuemodel = new mac_value().GetModel(Convert.ToInt32(DropDownList13.SelectedValue));
                mac_valuemodel.level2 = 1;
                new mac_value().Update(mac_valuemodel);

                Response.Write("<script>alert('传感器节点添加成功！请继续添加。');window.location = 'equipment_add"+adminnoss+".aspx?adminnos="+adminnoss+"';</script>");
            }
            else
            {
                Response.Write("<script>alert('安装图片未选择！');history.back();</script>");
            }
        }
    }
}